package mnnu.ghbe.controller;

import mnnu.ghbe.entity.Game;
import mnnu.ghbe.service.GameService;
import mnnu.ghbe.util.JwtUtil;
import mnnu.ghbe.dto.Result;
import mnnu.ghbe.dto.SubmitEventRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/simulator")
public class GameController {
    
    @Autowired
    private GameService gameService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    @PostMapping("/start")
    public Result<Game> startGame(@RequestHeader("Authorization") String token) {
        try {
            // 从JWT token中提取用户ID
            String jwtToken = token.replace("Bearer ", "");
            if (!jwtUtil.validateToken(jwtToken)) {
                return Result.error(401, "无效的token");
            }
            
            Long userId = jwtUtil.getUserIdFromToken(jwtToken);
            if (userId == null) {
                return Result.error(401, "无效的token");
            }
            
            // 开始游戏
            Game game = gameService.startGame(userId);
            
            return Result.success(game);
        } catch (Exception e) {
            return Result.error(500, "游戏开始失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/submit")
    public Result<GameService.GameEventResult> submitEvent(@RequestHeader("Authorization") String token,
                                                           @RequestBody SubmitEventRequest request) {
        try {
            // 从JWT token中提取用户ID
            String jwtToken = token.replace("Bearer ", "");
            if (!jwtUtil.validateToken(jwtToken)) {
                return Result.error(401, "无效的token");
            }
            
            Long userId = jwtUtil.getUserIdFromToken(jwtToken);
            if (userId == null) {
                return Result.error(401, "无效的token");
            }
            
            // 处理事件
            GameService.GameEventResult result = gameService.submitEvent(userId, request.getEventId(), request.getWithdrawAmount());
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(500, "事件处理失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/current")
    public Result<Game> getCurrentGameInfo(@RequestHeader("Authorization") String token) {
        try {
            // 从JWT token中提取用户ID
            String jwtToken = token.replace("Bearer ", "");
            if (!jwtUtil.validateToken(jwtToken)) {
                return Result.error(401, "无效的token");
            }
            
            Long userId = jwtUtil.getUserIdFromToken(jwtToken);
            if (userId == null) {
                return Result.error(401, "无效的token");
            }
            
            // 获取当前游戏信息
            Game game = gameService.getCurrentGameInfo(userId);
            
            if (game == null) {
                return Result.error(404, "游戏信息不存在");
            }
            
            return Result.success(game);
        } catch (Exception e) {
            return Result.error(500, "获取游戏信息失败: " + e.getMessage());
        }
    }
}